package com.oracle.util;

import com.oracle.pojo.Teacher;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class JJWTUtilsTeacher {
    /**
     * 过期时间4小时，单位毫秒
     */
    public static final long EXPIRE = 1000*60*60*4;

    /*
     * 秘钥(盐)
     * */
    public static final String SECRET = "ukc8BDbRigUDaY6pZFfWus2jZWLPHO1122";

    /**
     * claim user - 对象 -- 脱敏处理
     * 生成token字符串的方法
     * @return token字符串
     */
    public static String getJwtToken(Teacher teacher)  {
        return Jwts.builder()
                //JWT头信息
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS2256")
                //设置分类；设置过期时间 一个当前时间，一个加上设置的过期时间常量
                .setSubject("orl")
                .setIssuedAt(new Date())
//                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                //设置token主体信息，存储用户信息
                .claim("num", teacher.getTeachNum())
                .claim("id", teacher.getTeachId())
                .claim("name", teacher.getTeachName())
                .claim("tel", teacher.getTeachTel())
                .claim("head", teacher.getTeachHead())
                .claim("headList", teacher.getTeachHeadList())
                .signWith(SignatureAlgorithm.HS256, SECRET)
                .compact();
    }

    /**
     * 判断token是否存在与有效
     *
     * @param jwtToken  token串
     * @return 验证结果
     */
    public static boolean checkToken(String jwtToken){
        if (StringUtils.isBlank(jwtToken)){
            return false;
        }
        try{
            //验证token
            Jwts.parser().setSigningKey(SECRET).parseClaimsJws(jwtToken);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 判断token是否存在与有效
     *
     * @param request  request
     * @return 验证结果
     */
    public static boolean checkToken(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            if (StringUtils.isBlank(token)){
                return false;
            }
            Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 解密token
     *
     * @param request request
     * @return 解密结果map
     */
    public static Teacher getMemberIdByJwtToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return decode(token);
    }

    /**
     * 解密token
     *
     * @param token token字符串
     * @return 解密结果map
     */
    public static Teacher getMemberIdByJwtToken(String token){
        return decode(token);
    }

    public static Teacher decode(String token) {
        // 封装解密结果
        if (StringUtils.isEmpty(token)){
            return null;
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        String num =  (String)body.get("num");
        Integer id = Integer.valueOf(body.get("id") + "");
        String teacherName = null;
        if ((!ObjectUtils.isEmpty(body.get("name")))){
            teacherName = body.get("name") + "";
        }
        String teacherTel = null;
        if ((!ObjectUtils.isEmpty(body.get("tel")))){
            teacherTel = body.get("tel") + "";
        }
        String teacherHead = null;
        if ((!ObjectUtils.isEmpty(body.get("head")))){
            teacherHead = body.get("head") + "";
        }
        String teacherHeadList = null;
        if ((!ObjectUtils.isEmpty(body.get("headList")))){
            teacherHeadList = body.get("headList") + "";
        }
        Teacher teacher = new Teacher();
        teacher.setTeachId(id);
        teacher.setTeachNum(num);
        teacher.setTeachTel(teacherTel);
        teacher.setTeachName(teacherName);
        teacher.setTeachHead(teacherHead);
        teacher.setTeachHeadList(teacherHeadList);



        return teacher;
    }
}
